
local AvatarFactory = require("app.public.avatar.AvatarFactory")

local DebugOptionalCell = import(".DebugOptionalCell")

local TargetPlatform = cc.Application:getInstance():getTargetPlatform()

local DebugPanel = class("DebugPanel",function() return ccui.Layout:create() end)

local Optionals = {
	{default=true,desc="地图动画",exec=function(b) --@ignore
		MapManager:getMap():pauseAnimation(not b)
		NpcManager:pauseAllNpc(not b)
	end},

	{default=true,desc="UI界面开关",exec=function(b)  --@ignore
		UIHandler:getDesktopController():setVisible(b)
		Config.uiDisplay = b
	end},

	{default=true,desc="显示Npc",exec=function(b) --@ignore
		NpcManager:allNpcAbslutelyVisibility(b)
	end},

	{default=true,desc="显示其他玩家装备(切换地图后生效)",exec=function(b) --@ignore
		Config.ignoreOtherPlayerEquips = (not b)
	end},

	{default=true,desc="显示其他玩家坐骑(切换地图后生效)",exec=function(b) --@ignore
		Config.ignoreOtherPlayerMount = (not b)
	end},

	{default=cc.Director:getInstance():isDisplayStats(),desc="FPS开关",exec=function(b) --@ignore
		cc.Director:getInstance():setDisplayStats(b)
	end},

	{default=true,desc="允许剪裁",exec=function(b) --@ignore
		cc.SetClippingEnabled(b)
	end},

	{default=false,desc="DumpFontAtlasImages",exec=function(b) --@ignore
		if b then
			cc.DumpFontAtlasImages()
		end
	end},

	{default=false,desc="机器人",exec=function(b) --@ignore
		if b then
			FieldManager:startDebugUsers() 
		else
			FieldManager:stopDebugUsers()
		end
	end},
}

local ok,lmprof = pcall(require,"lmprof")
if ok then
	table.insert(Optionals,{
		default=lmprof.isRunning(),desc="内存分析开关",exec=function(b) --@ignore
			debug.profileMode = 0
			debug.profileDirty= true
		end})

	table.insert(Optionals,{
		default=false,desc="性能分析开关",exec=function(b) --@ignore
		debug.profileMode = 1
		debug.profileDirty= true
	end})
end


if TargetPlatform == cc.PLATFORM_OS_ANDROID then
	table.insert(Optionals,{
		default=false,desc="NDK-Profile开关",exec=function(b) --@ignore
		if b then
			cc.ProfileStart()
		else
			cc.ProfileEnd()
		end
	end})
end

function DebugPanel:ctor() 
	local size = cc.size(display.width*0.9, display.height*0.9)
	self:setContentSize(size)
	self:setAnchorPoint(cc.p(0.5,0.5))
	self:setPosition(display.cx,display.cy)

	local listView = ccui.ListView:create()
    listView:setDirection(ccui.ScrollViewDir.vertical)
    listView:setBounceEnabled(true)
    listView:setItemsMargin(5)
    listView:setContentSize(size.width*0.89,size.height*0.89)

    --[[
    local imageView = ccui.ImageView:create()
	imageView:loadTexture("ui_resources/commonuse/with_tips_bg1.png",1)
	imageView:setScale9Enabled(true)
	imageView:setContentSize(size.width*0.9,size.height*0.9)
	imageView:setPosition(listView:getPosition())
	imageView:setAnchorPoint(0,0)
	]]
	local bgView = ccui.Layout:create()
	bgView:setContentSize(size.width,size.height)
	bgView:setPosition(listView:getPosition())
	bgView:setAnchorPoint(0,0)
	bgView:setBackGroundColorType(1)
	bgView:setBackGroundColor(cc.c3b(96,128,255))

	local cellSize = cc.size(size.width*0.9,60)
	local layout = nil
	for i=1,#Optionals do
		local v = Optionals[i]
		if i%2==1 then
			layout = ccui.Layout:create()
			layout:setContentSize(cellSize)
		end
		local cell = DebugOptionalCell.new(cellSize,v.default,v.desc)
		cell:setSwitchCallback(function(b)
			v.exec(b)
			v.default=b
		end)
		layout:addChild(cell)
		if i%2==1 then
			listView:addChild(layout)
		else
			cell:setPositionX(cellSize.width*0.5)
		end
	end
	self:addChild(bgView)
	self:addChild(listView)


	local closeButton = ccui.Button:create(
		"public/public_black.png",
		"public/public_black.png",
		"public/public_black.png",
		0)
	closeButton:setScale9Enabled(true)
	closeButton:setContentSize(cc.size(200,80))
	closeButton:setPosition(size.width*0.5,size.height*0.96)
	closeButton:addClickEventListener(function(sender)
		self:removeFromParent()
	end)
	local text = ccui.Text:create()
	text:setString("点这里关闭") --@ignore
	text:setFontSize(28)
	text:setPosition(100,40)
	closeButton:addChild(text)
	self:addChild(closeButton)
end


return DebugPanel